/*
 * Copyright 2007-2008, www.jsemantic.org, www.adolfoestevez.com,
 * http://semanticj2ee.blogspot.com/ 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jsemantic.support.examples.even;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;

import org.jservicerules.support.semanticservice.StatelessSemanticService;
import org.jservicerules.support.spring.SpringContextFactory;
import org.springframework.context.ApplicationContext;

/**
 * The Class EvenStressTest.
 */
public class EvenStressTest {

	/** The Constant log. */
	private static final org.apache.log4j.Logger log = org.apache.log4j.Logger
			.getLogger(EvenStressTest.class);

	/** The ctx. */
	private static ApplicationContext ctx = SpringContextFactory
			.getClassPathApplicationContexFactory("org/jsemantic/support/examples/even/services.xml");

	/** The service. */
	private static StatelessSemanticService service = null;

	/**
	 * The Class ThreadHolder.
	 */
	public static class ThreadHolder implements Runnable {

		/* (non-Javadoc)
		 * @see java.lang.Runnable#run()
		 */
		public void run() {

			Collection results = service.execute(getFacts());

			if (log.isDebugEnabled()) {
				log.debug(results);
			}
			if (log.isInfoEnabled()) {
				log.info(results);
			}
			results.clear();
			results = null;

		}
	};

	/**
	 * The main method.
	 * 
	 * @param args the args
	 */
	public static void main(String[] args) {

		service = (StatelessSemanticService) ctx.getBean("evenService");

		while (true) {
			new Thread(new ThreadHolder()).start();
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		}

	}

	/**
	 * Gets the facts.
	 * 
	 * @return the facts
	 */
	private static Collection getFacts() {
		Random r = new Random();
		List facts = new ArrayList();
		for (int i = 0; i < 10; i++) {
			facts.add(new Integer(r.nextInt() % 100));
		}
		return facts;
	}

}
